#include "gameuicontainer.h"
#include <QHBoxLayout>
#include "UIEditor/uilib.h"
#include "UIEditor/uieditorwidget.h"
#include "gamemanager.h"
#include "Sound/soundmanager.h"
GameUIContainer::GameUIContainer()
{
    Init();
    this->setObjectName("gameuicontainer");
    this->setStyleSheet(qss);
    connect(&EventCenter::Instance(),&EventCenter::GoToWidget,this,[=](QString name){
        this->show();
        ChangeUI(name);
    });
}

GameUIContainer &GameUIContainer::Instance()
{
    static GameUIContainer instance;
    return instance;
}

GameUIContainer::~GameUIContainer()
{
    delete album;
    delete coverPage;
    delete record;
    delete dataView;
    delete menu;
    delete black;
    delete timer;
}

void GameUIContainer::ChangeUI(QString name)
{
    if(name == "游戏")
    {
        this->hide();
        return;
    }
    if(name == "black")
    {
        this->setStyleSheet("");
        this->show();
        if(!curUI.isEmpty() && myWidgets[curUI])
        {
            myWidgets[curUI]->hide();
        }
        black->setStyleSheet("");
        black->show();
        return;
    }
    this->setStyleSheet(qss);
    if(!curUI.isEmpty() && myWidgets[curUI])
    {
        myWidgets[curUI]->hide();
    }
    if(!name.isEmpty())
    {
        if(myWidgets[name])
        {
            myWidgets[name]->show();
        }
        curUI = name;
    }
    if(name == "封面")
    {
        //bgm
        SoundManager::Instance().PlayBGM(GameManager::Instance().bgmPath);
    }
}

void GameUIContainer::AddWidget(MyWidget *w)
{
    myWidgets.insert(w->name,w);
    this->layout()->addWidget(w);
    this->layout()->setContentsMargins(0,0,0,0);
    w->hide();
}

void GameUIContainer::Init()
{
    this->setLayout(new QHBoxLayout());
    album = new Album(this);
    coverPage = new CoverPage(this);
    record = new Record(this);
    dataView = new DataView(this);
    menu = new Menu(this);

    QMap<QString,QWidget*> map;
    map.insert("图鉴",album);
    map.insert("封面",coverPage);
    map.insert("存档",record);
    map.insert("数值",dataView);
    map.insert("菜单",menu);

    curUI = "封面";

    //读界面
    QString interfacePath = FileManager::Instance().uiEditorInterfacesPath;
    JsonTranslator jt;

    QJsonObject* widgetob = FileManager::Instance().ReadJsonFile(interfacePath);
    if(widgetob)
    {
        QJsonArray widgetArray = (*widgetob)["widgets"].toArray();
        for (int i = 0;i < widgetArray.count();i++)
        {
            QJsonObject* ob = new QJsonObject(widgetArray[i].toObject());
            MyWidget* w = jt.WidgetFromJson(ob);
            if(!w)
                continue;
            myWidgets.insert(w->name,w);
            this->layout()->addWidget(w);
            this->layout()->setContentsMargins(0,0,0,0);
            w->hide();
        }
    }
    for(auto it = map.begin();it != map.end(); it++)
    {
        if(!it.value())
            continue;
        if(!myWidgets[it.key()])
        {
            myWidgets[it.key()] = new MyWidget(this);
        }
        myWidgets[it.key()]->setLayout(new QHBoxLayout());
        myWidgets[it.key()]->layout()->setContentsMargins(0,0,0,0);
        myWidgets[it.key()]->layout()->addWidget(it.value());
        it.value()->setParent(myWidgets[it.key()]);
        it.value()->lower();
    }

    myWidgets[curUI]->show();
    //过渡界面
    timer = new QTimer();
    black = new QWidget();
    this->layout()->addWidget(black);
    this->layout()->setContentsMargins(0,0,0,0);
    black->hide();
    connect(&EventCenter::Instance(),&EventCenter::Black,[=](int ms){
        ChangeUI("black");
        totalTime = ms;
        timer->start(interval);
    });
    connect(&EventCenter::Instance(),&EventCenter::BlackLighter,[=](int ms){
        ChangeUI("black");
        black->setStyleSheet("background-color:black");
        accumulate = ms;
        up = false;
        totalTime = ms;
        timer->start(interval);
    });
    connect(timer,&QTimer::timeout,this,[=](){
        if(up)
        {
            accumulate += interval;
            float a = (float)accumulate / (float)totalTime;
            black->setStyleSheet("background-color:rgba(0,0,0," + QString::number(a) + ");");
            if(accumulate >= totalTime)
            {
                up = false;
            }
        }
        else
        {
            accumulate -= interval;
            float a = (float)accumulate / (float)totalTime;
            if(accumulate <= 0)
            {
                timer->stop();
                accumulate = 0;
                ChangeUI("游戏");
                up = true;
                return;
            }
            black->setStyleSheet("background-color:rgba(0,0,0," + QString::number(a) + ");");
        }
    });
}
